<template>
  <div class="tab-bar-item" @click="itemClick">
    <div v-if="!isActive">
      <slot name="item-icon"></slot>
    </div>
    <div v-else>
      <slot name="item-icon-active"></slot>
    </div>
    <div :style="activateStyle" :class="{active:isActive}">
      <slot name="item-text"></slot>
    </div>
  </div>
</template>

<script>
  export default {
    name: "TabBarItem",
    props: {
      link: String,
      activeColor: {
        type: String,
        default: "var(--color-tint)"
      }
    },
    methods: {
      itemClick() {
        if (this.$route.path !== this.link) {
          this.$router.replace(this.link)
        }
      }
    },
    data() {
      return {}
    },
    computed: {
      isActive() {
        return this.$route.path.indexOf(this.link) !== -1
      },
      activateStyle() {
        return this.isActive ? {color: this.activeColor} : {}
      }
    }
  }
</script>

<style scoped>
  .tab-bar-item {
    flex: 1;
    text-align: center;
    height: 46px;
    font-size: 14px;
    padding-top: 5px;
  }

  .tab-bar-item img {
    width: 20px;
    height: 20px;
    margin: 3px 0;
    vertical-align: middle;
  }

  .tab-bar-item p {
    margin: 0;
  }
</style>
